import Vue from "vue";
import App from "./App.vue";
import router from "./router";
import store from "./store";
import ElementUI from 'element-ui';
import 'element-ui/lib/theme-chalk/index.css';
import "./assets/css/common.css"
import qfSubMenu from "qf-sub-menu" //引入我们的组件
import "@/assets/css/el-reset.css"
import nprogress from "nprogress"
import 'nprogress/nprogress.css'


Vue.use(qfSubMenu)
router.beforeEach((to,from,next)=>{
  nprogress.start()
  //判断是否有token
  let token = localStorage.getItem('wqj-token');
  if(token){
    if(store.state.userMenu.length===0){
      //用户的路由信息（菜单信息）没有了，需要重新发起请求
      store.dispatch('FETCH_MENULIST')
      .then(()=>{
        //重新激活动态添加的开路由，replac：true不产生历史记录
        next({...to,replace:true})
      })
    }else{
      next();
    }
   
  }else{
    //代表用户没有登录
    if(to.path === "/login"){
      next();
    }else{
      next({ path: "/login" });
    }
  }
  
})

//全局后置钩子
router.afterEach((to,next)=>{
  nprogress.done()
})

// Vue.config.productionTip = false;
Vue.use(ElementUI);

new Vue({
  router,
  store,
  render: (h) => h(App),
}).$mount("#app");
